<?php

class PublicAction extends Action {

    public $loginMarked;

    /**
      +----------------------------------------------------------
     * 初始化
      +----------------------------------------------------------
     */
    public function _initialize() {
        header("Content-Type:text/html; charset=utf-8");
        header('Content-Type:application/json; charset=utf-8');
        $loginMarked = C("TOKEN");
        $this->loginMarked = md5($loginMarked['admin_marked']);
    }

    /**
      +----------------------------------------------------------
     * 验证token信息
      +----------------------------------------------------------
     */
    private function checkToken() {
        if (!M("Admin")->autoCheckToken($_POST)) {
            die(json_encode(array('status' => 0, 'info' => '令牌验证失败')));
        }
        unset($_POST[C("TOKEN_NAME")]);
    }

    public function index() {
        if (IS_POST) {
            $this->checkToken();
            $returnLoginInfo = D("Public")->auth();
            //生成认证条件
            if ($returnLoginInfo['status'] == 1) {
                $map = array();
                // 支持使用绑定帐号登录
                $map['email'] = $this->_post('email');
                import('ORG.Util.RBAC');
                $authInfo = RBAC::authenticate($map);
                $_SESSION[C('USER_AUTH_KEY')] = $authInfo['aid'];
                $_SESSION['email'] = $authInfo['email'];
                if ($authInfo['email'] == C('ADMIN_AUTH_KEY')) {
                    $_SESSION[C('ADMIN_AUTH_KEY')] = true;
                }
                // 缓存访问权限
                RBAC::saveAccessList();
            }
            echo json_encode($returnLoginInfo);
        } else {
            if (isset($_COOKIE[$this->loginMarked])) {
                $this->redirect("Index/index");
            }

            $systemConfig = include WEB_ROOT . 'Common/systemConfig.php';
            $this->assign("site", $systemConfig);
            $this->display("Common:login");
        }
    }

    public function loginOut() {
        setcookie("$this->loginMarked", NULL, -3600, "/");
        unset($_SESSION["$this->loginMarked"], $_COOKIE["$this->loginMarked"]);
        if (isset($_SESSION[C('USER_AUTH_KEY')])) {
            unset($_SESSION[C('USER_AUTH_KEY')]);
            unset($_SESSION);
            session_destroy();
        }
        $this->redirect("Index/index");
    }

    public function findPwd() {
        $M = M("Admin");
        if (IS_POST) {
            $this->checkToken();
            echo json_encode(D("Public")->findPwd());
        } else {
            setcookie("$this->loginMarked", NULL, -3600, "/");
            unset($_SESSION["$this->loginMarked"], $_COOKIE["$this->loginMarked"]);
            $cookie = $this->_get('code');
            $shell = substr($cookie, -32);
            $aid = (int) str_replace($shell, '', $cookie);
            $info = $M->where("`aid`='$aid'")->find();
            if ($info['status'] == 0) {
                $this->error("你的账号被禁用，有疑问联系管理员吧", __APP__);
            }
            if (md5($info['find_code']) == $shell) {
                $this->assign("info", $info);
                $_SESSION['aid'] = $aid;
                $systemConfig = include WEB_ROOT . 'Common/systemConfig.php';
                $this->assign("site", $systemConfig);
                $this->display("Common:findPwd");
            } else {
                $this->error("验证地址不存在或已失效", __APP__);
            }
        }
    }

    public function uploadify(){
        $verifyToken = md5('whoopsfeed_good' . $_POST['timestamp']);

        if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
            //上传图片
            import("ORG.Net.UploadFile");
            $upload = new UploadFile();
            $upload->maxSize  = 10485760 ;
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg','bmg');
            //设置需要生成缩略图，仅对图像文件有效
            $upload->thumb = true;
            //设置需要生成缩略图的文件后缀
            $upload->thumbPrefix = '';  //生产2张缩略图
            //设置缩略图最大宽度
            $upload->thumbMaxWidth = '320';
            //设置缩略图最大高度
            $upload->thumbMaxHeight = '200';

            //替换thumb图片
            if($_POST['curr_thumb']){
                $curr_thumb = explode('/',$_POST['curr_thumb']);
                $thumb_key = count($curr_thumb) -1;
                $curr_thumb1 = explode('.',$curr_thumb[$thumb_key]);

                $thumb_basename = $curr_thumb1[0];
                $upload->thumbFile = $thumb_basename;
                $thumb_extension = $curr_thumb1[1];
                $upload->thumbExt = $thumb_extension;

                $dir =  './';
                foreach($curr_thumb as $k => $v){
                    if($thumb_key != $k){
                        $dir .= $v . '/';
                    }
                }

                $upload->savePath =  str_replace('thumb/','',$dir);
                //设置缩略图路径
                $upload->thumbPath =  $dir;
            }else{
                $upload->thumbFile = date('YmdHis') . '_' . rand(1,999999999);
                $dir =  './Uploads/image/'.date('Ymd').'/';
                $upload->savePath = $dir;
                //设置缩略图路径
                $upload->thumbPath =  $dir.'thumb/';
            }
            //设置上传文件规则
            $upload->saveRule = 'uniqid';
            //删除原图
            $upload->thumbRemoveOrigin = true;
            if(!$upload->upload()) {
                $this->error($upload->getErrorMsg(), __APP__);
            }else{
                $info =  $upload->getUploadFileInfo();

                //echo  $info[0]['savename'];
                $thumb_ext = pathinfo($info[0]['savename']);
                echo $upload->thumbPath.$upload->thumbFile.'.'.$thumb_ext['extension'];
            }
        }else{
            $this->error("缺少参数", __APP__);
        }
    }

}